package com.tenebrous.jestic.controller.framwork.meber.organ;

import com.github.pagehelper.PageInfo;
import com.tenebrous.jestic.controller.SupperController;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgRoster;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgRosterHistory;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUser;
import com.tenebrous.jestic.service.framwork.meber.organ.OrgRosterService;
import com.rain.ops.sdk.dto.ApiResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工名册
 */
@Validated
@RestController
@RequestMapping("/organ/roster")
public class RosterController extends SupperController {

    @Autowired
    private OrgRosterService rosterService;

    @PostMapping("/standard/user/query")
    public ApiResult query(String hisid, String companyid, String departmentid, String politiCode, String edutionCode, String degreeCode, String sexCode, String realname) {
        String[] companyids = null;
        OrgRosterHistory hitem = null;
        if (StringUtils.isNotBlank(hisid)) {
            hitem = rosterService.getRosterHistoryItem(hisid);
        } else {
            hitem = rosterService.getDefultRosterHistoryItem();
        }
        if (hitem != null) {
            if (StringUtils.isNotBlank(companyid)) {
                companyids = new String[]{companyid};
            }
            List<OrgRoster> rlist = rosterService.queryStandardList(hitem.getId(), companyids, departmentid, politiCode, edutionCode, degreeCode, sexCode, realname);
            Map map = new HashMap();
            map.put("rosteritem", hitem);
            map.put("rosterlist", rlist);
            return new ApiResult(200, hitem.getName(), map);
        }
        return new ApiResult(999, "查询失败，未找到指定名册。");
    }

    @PostMapping("/standard/user/paging")
    public ApiResult paging(String hisid, String companyid, String departmentid, String politiCode, String edutionCode, String degreeCode, String sexCode, String realname) {
        String[] companyids = null;
        OrgRosterHistory hitem = null;
        if (StringUtils.isNotBlank(hisid)) {
            hitem = rosterService.getRosterHistoryItem(hisid);
        } else {
            hitem = rosterService.getDefultRosterHistoryItem();
        }
        if (hitem != null) {
            if (StringUtils.isNotBlank(companyid)) {
                companyids = new String[]{companyid};
            }
            PageInfo pageInfo = rosterService.queryStandardPagingList(super.getPageIndex(), super.getPageSize(), hitem.getId(), companyids, departmentid, politiCode, edutionCode, degreeCode, sexCode, realname);
            Map map = new HashMap();
            map.put("rosteritem", hitem);
            map.put("pageInfo", pageInfo);
            return new ApiResult(200, hitem.getName(), map);
        }
        return new ApiResult(999, "查询失败，未找到指定名册。");
    }

    /**
     * 向名册中添加人员
     *
     * @param hisid
     * @param userids
     * @return
     */
    @PostMapping("/standard/user/add")
    public ApiResult addStandardUser(@NotBlank String hisid, @NotEmpty String[] userids) {
        return rosterService.addStandardUser(hisid, userids, super.getCurrentUser());
    }

    /**
     * 更新名册
     *
     * @param hisid
     * @return
     */
    @PostMapping("/standard/user/update")
    public ApiResult updateStandardUser(@NotBlank String hisid) {
        return rosterService.updateStandardUser(hisid, super.getCurrentUser());
    }

    /**
     * 查找历史名称
     *
     * @param hisid
     * @return
     */
    @PostMapping("/standard/history/get")
    public ApiResult getHistory(@NotBlank String hisid) {
        OrgRosterHistory item = rosterService.getRosterHistoryItem(hisid);
        if (item != null) {
            return new ApiResult(200, "查找成功。", item);
        }
        return new ApiResult(999, "查找失败。");
    }

    @PostMapping("/standard/history/paging")
    public ApiResult paging() {
        return new ApiResult(200, "查询成功。", rosterService.queryHistoryPagingList(super.getPageIndex(), super.getPageSize(), "标准名册"));
    }

    @PostMapping("/standard/history/defult/update")
    public ApiResult updateDefult(@NotBlank String hisid) {
        List<OrgRosterHistory> hlist = rosterService.queryHistoryList();
        for (OrgRosterHistory hitem : hlist) {
            if (hisid.equals(hitem.getId())) {
                hitem.setActived(1);
            } else {
                hitem.setActived(0);
            }
        }
        return new ApiResult(200, "更新成功。", rosterService.updateHistorySelective(hlist));
    }

    /**
     * 新建/更新标准名册
     *
     * @param item
     * @return
     */
    @PostMapping("/standard/newas")
    public ApiResult newas(@NotNull OrgRosterHistory item) {
        OrgUser ouser = super.getCurrentUser();
        return rosterService.newasStandard(item, ouser);
    }

    /**
     * 标准名册另存为
     *
     * @param item
     * @param hisid
     * @return
     */
    @PostMapping("/standard/saveas")
    public ApiResult saveas(@NotNull OrgRosterHistory item, @NotBlank String hisid) {
        OrgUser ouser = super.getCurrentUser();
        return rosterService.saveasStandard(item, hisid, ouser);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/standard/user/delete")
    public ApiResult delete(@NotEmpty String[] ids) {
        return new ApiResult(200, "移除成功。", rosterService.deleteByIds(ids));
    }

    /**
     * 清空名册中的全部人员
     *
     * @param hisid
     * @return
     */
    @PostMapping("/standard/user/clear")
    public ApiResult clear(@NotBlank String hisid) {
        return new ApiResult(200, "移除成功。", rosterService.clearByHisid(hisid));
    }

    /**
     * 刪除歷史名册
     *
     * @param ids
     * @return
     */
    @PostMapping("/standard/history/delete")
    public ApiResult deleteHistory(@NotEmpty String[] ids) {
        return new ApiResult(200, "刪除成功。", rosterService.deleteHistoryByIds(ids));
    }
}
